﻿using System.Threading;
using Common.Logging;
using Rmq.Center;
using User.Api.Helpers;

namespace Out.TaskRunner.RmqWorkers
{
    public class UserMessageWorker : IWorker
    {
        private static readonly ILog Logger = LogManager.GetLogger(typeof(UserMessageWorker));

        public void Start(CancellationToken ct)
        {
            if (ct.IsCancellationRequested)
            {
                return;
            }

            using (var consumer = new UserMessageConsumer())
            {
                consumer.Subscribe((id, count, dt, msg, channel) =>
                {
                    if (msg.Type == UserMessageType.挂号成功通知)
                    {
                        return ProcessRegSucceedMsg(msg.OrderId, channel);
                    }
                    if (msg.Type == UserMessageType.挂号失败通知)
                    {
                        return ProcessRegFailedMsg(msg.OrderId, channel);
                    }
                    if (msg.Type == UserMessageType.挂号失败退费成功通知)
                    {
                        return ProcessRegFailedRefundSucceedMsg(msg.OrderId, channel);
                    }
                    if (msg.Type == UserMessageType.退号成功通知)
                    {
                        return ProcessUnpickupSucceedMsg(msg.OrderId, channel);
                    }
                    if (msg.Type == UserMessageType.退号失败通知)
                    {
                        return ProcessUnpickupFailedMsg(msg.OrderId, channel);
                    }
                    if (msg.Type == UserMessageType.退号成功退费成功通知)
                    {
                        return ProcessUnpickupSucceedRefundSucceedMsg(msg.OrderId, channel);
                    }
                    if (msg.Type == UserMessageType.缴费成功通知)
                    {
                        return ProcessBillSucceedMsg(msg.OrderId, channel);
                    }
                    if (msg.Type == UserMessageType.缴费失败通知)
                    {
                        return ProcessBillFailedMsg(msg.OrderId, channel);
                    }
                    if (msg.Type == UserMessageType.缴费失败退款成功通知)
                    {
                        return ProcessBillFailedRefundSucceedMsg(msg.OrderId, channel);
                    }
                    if (msg.Type == UserMessageType.住院预缴金成功通知)
                    {
                        return ProcessPrepaySucceedMsg(msg.OrderId, channel);
                    }
                    if (msg.Type == UserMessageType.住院预缴金失败通知)
                    {
                        return ProcessPrepayFailedMsg(msg.OrderId, channel);
                    }
                    if (msg.Type == UserMessageType.住院预缴金失败退费成功通知)
                    {
                        return ProcessPrepayFailedRefundSucceedMsg(msg.OrderId, channel);
                    }

                    if (msg.Type == UserMessageType.点餐成功通知)
                    {
                        return ProcessDinnerSucceedMsg(msg.OrderId, channel);
                    }
                    if (msg.Type == UserMessageType.点餐失败通知)
                    {
                        return ProcessDinnerFailedMsg(msg.OrderId, channel);
                    }
                    if (msg.Type == UserMessageType.点餐失败退费成功通知)
                    {
                        return ProcessDinnerFailedRefundSucceedMsg(msg.OrderId, channel);
                    }
                    if (msg.Type == UserMessageType.退餐成功通知)
                    {
                        return ProcessUnDinnerSucceedMsg(msg.OrderId, channel);
                    }
                    if (msg.Type == UserMessageType.退餐失败通知)
                    {
                        return ProcessUnDinnerFailedMsg(msg.OrderId, channel);
                    }
                    if (msg.Type == UserMessageType.退餐成功退费成功通知)
                    {
                        return ProcessUnDinnerSucceedRefundSucceedMsg(msg.OrderId, channel);
                    }

                    return new ConsumedResult { Ack = true };
                });

                while (true)
                {
                    if (ct.IsCancellationRequested)
                    {
                        break;
                    }

                    Thread.Sleep(50);
                }
            }
        }

        public ConsumedResult ProcessRegSucceedMsg(int orderId, WrapperChannel channel)
        {
            MessageHelper.SendRegSucceedMsg(orderId);

            return new ConsumedResult
            {
                Ack = true
            };
        }

        public ConsumedResult ProcessRegFailedMsg(int orderId, WrapperChannel channel)
        {
            MessageHelper.SendRegFailedMsg(orderId);

            return new ConsumedResult
            {
                Ack = true
            };
        }

        public ConsumedResult ProcessRegFailedRefundSucceedMsg(int orderId, WrapperChannel channel)
        {
            MessageHelper.SendRegFailedRefundSucceedMsg(orderId);

            return new ConsumedResult
            {
                Ack = true
            };
        }

        public ConsumedResult ProcessUnpickupSucceedMsg(int orderId, WrapperChannel channel)
        {
            MessageHelper.SendUnpickupSucceedMsg(orderId);

            return new ConsumedResult
            {
                Ack = true
            };
        }

        public ConsumedResult ProcessUnpickupFailedMsg(int orderId, WrapperChannel channel)
        {
            MessageHelper.SendUnpickupFailedMsg(orderId);

            return new ConsumedResult
            {
                Ack = true
            };
        }

        public ConsumedResult ProcessUnpickupSucceedRefundSucceedMsg(int orderId, WrapperChannel channel)
        {
            MessageHelper.SendUnpickupSucceedRefundSucceedMsg(orderId);

            return new ConsumedResult
            {
                Ack = true
            };
        }

        public ConsumedResult ProcessBillSucceedMsg(int orderId, WrapperChannel channel)
        {
            MessageHelper.SendBillSucceedMsg(orderId);

            return new ConsumedResult
            {
                Ack = true
            };
        }

        public ConsumedResult ProcessBillFailedMsg(int orderId, WrapperChannel channel)
        {
            MessageHelper.SendBillFailedMsg(orderId);

            return new ConsumedResult
            {
                Ack = true
            };
        }

        public ConsumedResult ProcessBillFailedRefundSucceedMsg(int orderId, WrapperChannel channel)
        {
            MessageHelper.SendBillFailedRefundSucceedMsg(orderId);

            return new ConsumedResult
            {
                Ack = true
            };
        }

        public ConsumedResult ProcessPrepaySucceedMsg(int orderId, WrapperChannel channel)
        {
            MessageHelper.SendPrepaySucceedMsg(orderId);

            return new ConsumedResult
            {
                Ack = true
            };
        }

        public ConsumedResult ProcessPrepayFailedMsg(int orderId, WrapperChannel channel)
        {
            MessageHelper.SendPrepayFailedMsg(orderId);

            return new ConsumedResult
            {
                Ack = true
            };
        }

        public ConsumedResult ProcessPrepayFailedRefundSucceedMsg(int orderId, WrapperChannel channel)
        {
            MessageHelper.SendPrepayFailedRefundSucceedMsg(orderId);

            return new ConsumedResult
            {
                Ack = true
            };
        }

        public ConsumedResult ProcessDinnerSucceedMsg(int orderId, WrapperChannel channel)
        {
            MessageHelper.SendDinnerSucceedMsg(orderId);

            return new ConsumedResult
            {
                Ack = true
            };
        }

        public ConsumedResult ProcessDinnerFailedMsg(int orderId, WrapperChannel channel)
        {
            MessageHelper.SendDinnerFailedMsg(orderId);

            return new ConsumedResult
            {
                Ack = true
            };
        }

        public ConsumedResult ProcessDinnerFailedRefundSucceedMsg(int orderId, WrapperChannel channel)
        {
            MessageHelper.SendRegFailedRefundSucceedMsg(orderId);

            return new ConsumedResult
            {
                Ack = true
            };
        }

        public ConsumedResult ProcessUnDinnerSucceedMsg(int orderId, WrapperChannel channel)
        {
            MessageHelper.SendUnDinnerSucceedMsg(orderId);

            return new ConsumedResult
            {
                Ack = true
            };
        }

        public ConsumedResult ProcessUnDinnerFailedMsg(int orderId, WrapperChannel channel)
        {
            MessageHelper.SendUnDinnerFailedMsg(orderId);

            return new ConsumedResult
            {
                Ack = true
            };
        }

        public ConsumedResult ProcessUnDinnerSucceedRefundSucceedMsg(int orderId, WrapperChannel channel)
        {
            MessageHelper.SendUnDinnerSucceedRefundSucceedMsg(orderId);

            return new ConsumedResult
            {
                Ack = true
            };
        }

    }
}